package studysystem.demo.controller;


import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.codec.Hex;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import studysystem.demo.bean.Depend;
import studysystem.demo.mapper.DependMapper;
import studysystem.demo.tool.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.MessageDigest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.List;

@Controller
public class DependController {
    @Autowired
    private DependMapper dependMapper;

    //查询所有小类  测试OK
    @CrossOrigin
    @RequestMapping(value="/all/depend", method = RequestMethod.GET)
    @ResponseBody
    public MyResp AllDependList(){
        PageHelper.startPage(1, 10);
        List<Depend> list=dependMapper.findAllDepend();
        for(int i=0;i<list.size();i++){
            list.get(i).setCourse_dir(dependMapper.findCourse_dirById(dependMapper.findClassByname(list.get(i).getName())));
        }
        return MyResp.render(0,"获取成功", list, dependMapper.findAllDependCount());
    }

    //查询某个小类   测试OK
    @CrossOrigin
    @RequestMapping(value="/all/depend/{name}", method = RequestMethod.GET)
    @ResponseBody
    public MyResp DependList(@PathVariable String name){
        PageHelper.startPage(1, 10);
        if(name == null || name == ""){
            List<Depend> list=dependMapper.findAllDepend();
            for(int i=0;i<list.size();i++){
                list.get(i).setCourse_dir(dependMapper.findCourse_dirById(dependMapper.findClassByname(list.get(i).getName())));
            }
            return MyResp.render(0,"获取成功", list, dependMapper.findAllDependCount());
        }else if(dependMapper.findDependByname(name).size()==0){
            List<Depend> list=dependMapper.findAllDepend();
            for(int i=0;i<list.size();i++){
                list.get(i).setCourse_dir(dependMapper.findCourse_dirById(dependMapper.findClassByname(list.get(i).getName())));
            }
            return MyResp.render(0,"课程不存在!",list, dependMapper.findAllDependCount());
        }else {
            List<Depend> list=dependMapper.findAllDependByName(name);
            for(int i=0;i<list.size();i++){
                list.get(i).setCourse_dir(dependMapper.findCourse_dirById(dependMapper.findClassByname(list.get(i).getName())));
            }
            return  MyResp.render(0,"获取成功",list, dependMapper.findAlldDependCountByName(name));
        }
    }

    //查询某个小类的依赖关系   测试OK
    @CrossOrigin
    @RequestMapping(value="/course/depend/{name}", method = RequestMethod.GET)
    @ResponseBody
    public MyResp findDependByName(@PathVariable String name){
        PageHelper.startPage(1, 10);
        List<Depend> list=dependMapper.findCourseDependByName(name);
        for(int i=0;i<list.size();i++) {
            list.get(i).setCourse_dir(dependMapper.findCourse_dirById(dependMapper.findClassByname(list.get(i).getName())));
            if(list.get(i).getLastcourse()==0){
                list.get(i).setLastcourse_name("无");
            }else{
                list.get(i).setLastcourse_name(dependMapper.findCourseById(list.get(i).getLastcourse()));
            }
            if(list.get(i).getNextcourse()==0){
                list.get(i).setNextcourse_name("无");
            }else{
                list.get(i).setNextcourse_name(dependMapper.findCourseById(list.get(i).getNextcourse()));
            }
        }
        return  MyResp.render(0,"获取成功",list,1);

    }

    //修改某个小类的依赖关系
    @CrossOrigin
    @RequestMapping(value = "/update/depend",method = RequestMethod.POST)
    @ResponseBody
    public Mess UpdateDoc(@RequestBody Depend dependEntity){
        String b;
        if(dependEntity.getLastcourse_name()==null||dependEntity.getLastcourse_name()==""){
            dependEntity.setLastcourse_name("无");
            dependEntity.setLastcourse(0);
        }
        if(dependEntity.getNextcourse_name()==null||dependEntity.getNextcourse_name()==""){
            dependEntity.setNextcourse_name("无");
            dependEntity.setNextcourse(0);
        }
        if(!dependEntity.getLastcourse_name().equals("无")&&dependMapper.findDependByname2(dependEntity.getLastcourse_name())==null){
            b="LastCourse不存在!";
        }else if(!dependEntity.getNextcourse_name().equals("无")&&dependMapper.findDependByname2(dependEntity.getNextcourse_name())==null){
             b="NextCoourse不存在!";
        }else{
             if(!dependEntity.getLastcourse_name().equals("无")){
                 dependEntity.setLastcourse(dependMapper.findCourseIdByName(dependEntity.getLastcourse_name()));
             }
            if(!dependEntity.getNextcourse_name().equals("无")){
                dependEntity.setNextcourse(dependMapper.findCourseIdByName(dependEntity.getNextcourse_name()));
            }
            dependMapper.updateDepend(dependEntity);
            b="修改成功！";
        }
        return Mess.info(b);
    }
}
